package com.atguigu.srb.core.controller.api;


import com.atguigu.common.results.R;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.pojo.entity.LendItem;
import com.atguigu.srb.core.pojo.vo.InvestVO;
import com.atguigu.srb.core.service.LendItemService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 标的出借记录表 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2022-10-24
 */
@RestController
@RequestMapping("/api/core/lendItem")
public class ApiLendItemController {
    @Autowired
    LendItemService lendItemService;
    @ApiOperation("查询标的的投资记录")
    @GetMapping("lendItems/{lendId}")
    public R lendItems(@PathVariable("lendId")String lendId){
        List<LendItem> lendItems = lendItemService.list(new LambdaQueryWrapper<LendItem>()
                .eq(LendItem::getLendId, lendId)
                .ne(LendItem::getStatus, 0));
        return R.success().data("items",lendItems);
    }
    ///api/core/lendItem/notify
    @ApiOperation("投资后的回调接口")
    @PostMapping("notify")
    public String investNotify(HttpServletRequest request){
        String result = lendItemService.investNotify(request);
        return result;
    }

    //根据前端用户提交的投资请求  将投资相关的数据封装为一条记录存到lend_item表中
    @ApiOperation("投资")
    @PostMapping("/auth/commitInvest")
    public R commitInvest(@RequestBody InvestVO investVO ,
                          @RequestHeader(value = "token",required = false) String token){
        String formStr = lendItemService.commitInvest(investVO, token);
        return R.success().data("formStr",formStr);
    }
}

